package cassys.actions.login;

import cassys.application.DTOs.UserDTO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;


import cassys.domain.user.UserRoleMapping;
import cassys.ejb.clients.LoginManagementDelegate;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;

/**
 * <code>Set welcome message.</code>
 */
public class LoginAction extends ActionSupport
        implements Validateable, ModelDriven<UserDTO> {

    private LoginManagementDelegate loginDelegate;
    private UserDTO userDTO = new UserDTO();
    private static final String ADMIN = "admin";
    private static final String LECTURER = "lecturer";
    private static final String STUDENT = "student";

    public LoginAction() {
        loginDelegate = new LoginManagementDelegate();

    }

    @Override
    public String execute() throws Exception {
        String userId = userDTO.getUserId();
        String password = userDTO.getPassword();
        if ((userId == null) || (userId.equals("")) || (password == null) || (password.equals(""))) {
            return "input";
        }
        System.out.println("Autenticating");
        List<UserRoleMapping> userRoleList = loginDelegate.validateUser(userId, password);
        System.out.println("Roles count: "+ userRoleList.get(0).getRole());
        if ((userRoleList != null) && (userRoleList.size() > 0)) {
            Map session = ActionContext.getContext().getSession();
            session.put("logged-in", "true");
            session.put("userObj", userRoleList.get(0).getUserSeqNo());
            List<String> roles = new ArrayList(3);
            for (UserRoleMapping userRole : userRoleList) {
                roles.add(userRole.getRole().toUpperCase());
            }
            
            if (roles.contains("ADMIN")) {
                System.out.println("ADMIN");
                userDTO = new UserDTO();
                return ADMIN;
            } else if (roles.contains("LECTURER")) {
                System.out.println("Lecturer");
                return LECTURER;
            } else if (roles.contains("STUDENT")) {
                return STUDENT;
            }
            return ERROR;
        } else {
            return ERROR;
        }
    }

    public String goToHome() throws Exception {
        
        return SUCCESS;
    }

    public String logout() throws Exception {

        Map session = ActionContext.getContext().getSession();
        // session.remove("logged-in");
        // UserRoleMapping userObj = (UserRoleMapping) session.get(0);
        // System.out.println("getName---;----ServletActionContext-----------" + userObj.getName());
        //  System.out.println("userId---;----ServletActionContext-----------" + userObj.getUserId()) ;

        session.remove("userObj");
        return SUCCESS;
    }

    @Override
    public UserDTO getModel() {
        return this.userDTO;
    }

    /* public String validateUser() {
    try {
    request = ServletActionContext.getRequest();
    Map session = new HashMap(1);
    String userId = loginDTO.getUserId();
    String password = loginDTO.getPassword();
    //  userId = (String) request.getParameter(userId);
    System.out.println("userId-------ServletActionContext-----------" + userId);
    System.out.println("req-------ServletActionContext-----------" + (String) request.getParameter(userId));
    if (isInValid(userId)) {
    return ERROR;
    }
    if (isInValid(password)) {
    return ERROR;
    }
    System.out.println("userId------------------" + userId);
    UserRoleMapping userObj = (UserRoleMapping) loginDelegate.validateUser(userId, password);
    request.setAttribute("userObj", userObj);
    System.out.println("request");
    System.out.println("request" + request.getAttribute("userObj"));
    setServletRequest(request);
    session.put("userObj", userObj);
    setSession(session);
    return SUCCESS;
    } catch (IndexOutOfBoundsException e) {
    //throw new RuntimeException(e);
    } catch (Exception e) {
    //throw new RuntimeException(e);
    }
    return ERROR;
    }*/
}
